package com.zoweunion.mechanic.controller;

import org.apache.shiro.ShiroException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice//全局处理`Spring Boot`的抛出异常
public class ExceptionController extends BaseController{
	private Logger logger = LoggerFactory.getLogger(ExceptionController.class);
	
	 // 捕捉shiro的异常
    @ExceptionHandler(ShiroException.class)
    public ResponseBean handle401(ShiroException e) {
    	return new ResponseBean(Constants.CODE_ACCESS_PROHIBITED,"权限不足",null);
    }
    
    @ExceptionHandler(MyException.class)
    public ResponseBean handleMyException(MyException e) {
    	return new ResponseBean(e.getCode(),e.getMsg(),null);
    }
    
    
    // 捕捉其他所有异常
    @ExceptionHandler(Exception.class)
    public ResponseBean globalException(HttpServletRequest request, Throwable ex) {
    	ex.printStackTrace();
        logger.error(ex.getMessage());
    	return new ResponseBean(Constants.CODE_ERROR, "操作失败",null);
    }


}

